package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Menu;
import com.example.demo.entity.RoleMenu;
import com.example.demo.entity.UserRole;
import com.example.demo.mapper.RoleMenuMapper;
import com.example.demo.mapper.UserRoleMapper;
import com.example.demo.service.MenuService;
import com.example.demo.service.RoleMenuService;
import com.example.demo.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author moguai
 * @version 1.0
 * @date 2022/3/19 20:49
 */
@Service
public class RoleMenuServiceImpl extends ServiceImpl<RoleMenuMapper, RoleMenu> implements RoleMenuService {
    @Autowired
    MenuService menuService;
    @Autowired
    @Qualifier(value = "userRoleServiceImpl")
    UserRoleService user_roleService;
    @Autowired(required = false)
    UserRoleMapper user_roleMapper;
    @Autowired(required = false)
    RoleMenuMapper user_menuMapper;

    @Override
    public List<Menu> SelectMenubyUser(Integer user_id) {
        //先找role
        QueryWrapper roleWrapper = new QueryWrapper();
        roleWrapper.eq("user_id", user_id);
        List<UserRole> user_roles = user_roleMapper.selectList(roleWrapper);
        List<Integer> roleId = user_roles.stream().map(UserRole::getRole_id).collect(Collectors.toList());
        //再找menu
        if(null == roleId || roleId.size() ==0 ){
            return null;
        }
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.in("role_id", roleId);
        List<RoleMenu> list = user_menuMapper.selectList(wrapper);
        List<Integer> Menu_id = list.stream().map(RoleMenu::getMenu_id).collect(Collectors.toList());
        return menuService.SelectMenus(Menu_id);
    }
}
